package club.adger.controller;

import club.adger.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.io.PrintStream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Adger
 * @Date: 2020/08/19/16:37
 * @Controller 交给IOC容器管理，同时具备了控制器的功能
 */
@Controller
@RequestMapping("/hello")
public class HelloHandler {

    /**
     * params中是多个参数就用 {}
     *
     * @return
     */
    @RequestMapping(value = "/index",method = RequestMethod.GET,params = {"id","name"})
    public String index(@RequestParam("id") int i,@RequestParam("name") String n){
        final PrintStream out = System.out;
        //打印输出id值和name
        out.println(i);
        out.println(n);
        out.println("执行了index请求...");
        return "index";
    }

    /**
     * @PathVariable 代表浏览器地址输入 与下面参数 绑定
     * @param id
     * @param name
     * @return
     */
    @RequestMapping("/rest/{id}/{name}")
    public String rest(@PathVariable("id") int id,@PathVariable("name") String name){
        System.out.println(id+"\t"+name);
        return "index";
    }

    @RequestMapping("/cookie")
    public String cookie(@CookieValue(value = "JSESSIONID") String sessionId){
        System.out.println(sessionId);
        return "index";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public String save(User user){
        System.out.println(user);
        return "index";
    }

    @RequestMapping("/forward")
    public String forward(){
        // 相当于 return "index";
        return "forward:/index.jsp";
    }

    @RequestMapping("/redirect")
    public String redirect(){
        //转发跟重定向像 这么 写的话 必须加后最 不然找不到的
        return "redirect:/index.jsp";
    }
}
